<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;
use App\Models\Cxhshop\GoodCategory;

class Goods extends CxhshopBase
{

    protected $table = 'ims_ewei_shop_goods';

    protected $fillable = [];

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];

    /**
     * 获取商品列表
     * param1 string $condition
     */
    public function getList($params)
    {
        //isrecommand推荐商品  isnew新品上市  ishot热卖商品  isdiscount促销商品  issendfree卖家包邮  istime限时抢购
        $query = self::query();
        $list = $query->whereChecked(0)->whereStatusGt(0)->whereDeleted(0);

        $pagenNum = isset($params['page']) ? $params['page'] : 1;
        $limit = isset($params['limit']) ? $params['limit'] : 10;
        $page = $pagenNum - 1;
        $page = $limit * $page;


        if (isset($params['condition'])) {
            $condition = json_decode($params['condition'], true);
        }

        // dd($condition);
        if (isset($condition['isrecommand'])) {
            $isrecommand = intval($condition['isrecommand']);
        }
        if (isset($condition['isnew'])) {
            $isnew = intval($condition['isnew']);
        }
        if (isset($condition['ishot'])) {
            $ishot = intval($condition['ishot']);
        }
        if (isset($condition['isdiscount'])) {
            $isdiscount = intval($condition['isdiscount']);
        }
        if (isset($condition['issendfree'])) {
            $issendfree = intval($condition['issendfree']);
        }
        if (isset($condition['istime'])) {
            $istime = intval($condition['istime']);
        }


        if(isset($params['sales'])) {
            $query -> orderBy('sales', 'DESC');
        }
        if(isset($params['marketprice'])) {
            if($params['marketprice'] == 1) {
                $query -> orderBy('marketprice', 'ASC');
            } else if($params['marketprice'] == 2) {
                $query -> orderBy('marketprice', 'DESC');
            }
        }


        isset($isrecommand) && $query->whereIsrecommand($isrecommand);
        isset($isnew) && $query->whereIsnew($isnew);
        isset($ishot) && $query->whereIshot($ishot);
        isset($isdiscount) && $query->whereIsDiscount($isdiscount);
        isset($issendfree) && $query->whereIssendfree($issendfree);
        isset($istime) && $query->whereIstime($istime);
        $all_cates = GoodCategory::getAllCategory(1);
        $all_cates = json_encode($all_cates, JSON_UNESCAPED_UNICODE);
        $categories = GoodCategory::getAllCategory();

        isset($params['cate']) && $query->queryCategory($params['cate'], $categories);

        $list = $query -> offset($page) -> limit($limit) -> get(['title', 'marketprice', 'thumb','id']) -> toArray();

        return ['list'=>$list, 'cates'=>$all_cates];

    }

    /**
     * 分类筛选
     * param1 $query
     * param2 int $cate
     * param3 array $category
     */
    public function scopeQueryCategory($query, $cate, $category)
    {

        $catearr = array(intval($cate));
        foreach ($category as $index => $row) {
            if ($row['parentid'] == $cate) {
                $catearr[] = $row['id'];
                foreach ($category as $ind => $ro) {
                    if ($ro['parentid'] == $row['id']) {
                        $catearr[] = $ro['id'];
                    }
                }
            }

        }

        $catearr = array_unique($catearr);
        $temp = ' cates like '."'%$catearr[0]%'";
        foreach($catearr as $key => $value) {

            if($key != 0) {
                $temp .= ' or cates like '."'%$value%'";
            } 
        }

        return $query -> whereRaw($temp);

    }

    /**
     * 获取商品列表
     * param1 int $id
     */
    public function getDetail($id) {

        $good = self::find($id);
        dd($good);

    }

    public function scopeWhereStatusGt($query, $number)
    {

        return $query->where('status', '>', $number);

    }

}
